package com.wdc;

import com.wdc.pojo.User;
import com.wdc.service.UserService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Scanner;

@Component
public class ConsoleRunner implements CommandLineRunner {

    private final UserService userService;
    private final Scanner scanner = new Scanner(System.in);

    public ConsoleRunner(UserService userService) {
        this.userService = userService;
    }

    @Override
    public void run(String... args) {
        showMenu();
        while (true) {
            System.out.print("请输入操作编号：");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消费换行符，避免影响后续输入

            switch (choice) {
                case 1:
                    queryAllUsers();
                    break;
                case 2:
                    queryUserById();
                    break;
                case 3:
                    addUser();
                    break;
                case 4:
                    deleteUserById();
                    break;
                case 5:
                    System.out.println("退出系统～");
                    return; // 退出循环，结束程序
                default:
                    System.out.println("无效操作，请重新输入！");
            }
            showMenu(); // 每次操作后重新显示菜单
        }
    }

    // 显示操作菜单
    private void showMenu() {
        System.out.println("\n=== 用户管理系统 ===");
        System.out.println("1. 查询所有用户");
        System.out.println("2. 按 ID 查询用户");
        System.out.println("3. 添加新用户");
        System.out.println("4. 按 ID 删除用户");
        System.out.println("5. 退出系统");
        System.out.println("===================");
    }

    // 1. 查询所有用户
    private void queryAllUsers() {
        List<User> users = userService.listAllUsers();
        if (users.isEmpty()) {
            System.out.println("当前无用户数据～");
        } else {
            System.out.println("所有用户列表：");
            users.forEach(System.out::println);
        }
    }

    // 2. 按 ID 查询用户
    private void queryUserById() {
        System.out.print("请输入要查询的用户 ID：");
        Integer id = scanner.nextInt();
        User user = userService.getUserById(id);
        if (user == null) {
            System.out.println("未找到 ID 为 " + id + " 的用户～");
        } else {
            System.out.println("查询到用户：" + user);
        }
    }

    // 3. 添加新用户
    private void addUser() {
        System.out.print("请输入用户姓名：");
        String name = scanner.nextLine();
        System.out.print("请输入用户性别：");
        String sex = scanner.nextLine();
        System.out.print("请输入用户年龄：");
        Integer age = scanner.nextInt();

        User user = new User();
        user.setName(name);
        user.setSex(sex);
        user.setAge(age);
        // ID 可让数据库自增（若表设计了自增，需修改 Mapper/XML 去掉 ID 入参）
        // 这里示例手动输入 ID，实际建议数据库自增
        System.out.print("请输入用户 ID：");
        user.setId(scanner.nextInt());

        int result = userService.addUser(user);
        if (result > 0) {
            System.out.println("添加用户成功！新增用户：" + user);
        } else {
            System.out.println("添加用户失败，请检查输入～");
        }
    }

    // 4. 按 ID 删除用户
    private void deleteUserById() {
        System.out.print("请输入要删除的用户 ID：");
        Integer id = scanner.nextInt();
        int result = userService.removeUser(id);
        if (result > 0) {
            System.out.println("删除 ID 为 " + id + " 的用户成功！");
        } else {
            System.out.println("删除失败，未找到该用户或数据库异常～");
        }
    }
}